/**
 * Created by Panda on 16/01/2017
 */
(function () {
    'use strict';

    var topBtns = [{
        key: "",
        icon: "plus",
        text: "添加"
    }, {
        key: "",
        icon: "delete",
        text: "删除"
    }, {
        key: "",
        icon: "iconfontimport",
        text: "导入"
    }, {
        key: "",
        icon: "iconfontexport",
        text: "导出"
    }, {
        key: "",
        icon: "eye",
        text: "查看"
    }];

    angular
        .module('com.akxx.aqzyk')
        .controller('StudentController', StudentController);


    /** @ngInject */
    function StudentController($timeout, $state, Storage,SweetAlert,_Student) {
        var vm = this;
        vm.newFrame = false;
        vm.newNav = false;
        vm.overlay = false;
        vm.newStu = {};
        vm.newClass = {};
        vm.isReadonly = false;
        getLeftbarList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        function getLeftbarList(param) {
            _Student.ClassList.get(param).then(function (res) {
                vm.naviBInit = res.data;
                vm.active = vm.naviBInit[0].id;
                var searchCode = vm.naviBInit[0].code;
                getStudentInfo({async: true,sortExpr:"name desc","q[classes]":searchCode});
            });
        };
        vm.topBtns = topBtns;

        function getStudentInfo(param){
            _Student.StudentList.get(param).then(function (res) {
                vm.student = res.data;
            });
        }

        // 左侧切换事件
        vm.LeftNavClickEvent = function (id) {
            vm.active = id;
            for(var i=0;i<vm.naviBInit.length;i++){
                if(vm.naviBInit[i].id == id){
                    var searchCode = vm.naviBInit[i].code;
                    console.log(searchCode);
                    getStudentInfo({async: true,sortExpr:"name desc","q[fld op]":searchCode});
                }
            }
        };
        //左侧hover事件
        vm.leftItemShow = function(id){
            vm.isShow = id;
        }
        vm.leftItemHide = function(id){
            vm.isShow = "";
        }
        //右侧hover事件
        vm.stuItemShow = function(id){
            vm.isShow = id;
        }
        vm.stuItemHide = function(id){
            vm.isShow = "";
        }

        //头部按钮事件
        vm.topBtnEvent = function(text){
            vm.newFrame = true;
            vm.overlay = true;
            switch (text) {
                case "添加":
                    vm.saveType = "new";
                    break;

            }
        }
        //关闭弹窗
        vm.closeFrame = function(){
            vm.newNav = false;
            vm.newFrame = false;
            vm.overlay = false;
            vm.isReadonly = false;
        };
        //新建左侧导航
        vm.addLeftNav = function(){
            vm.newNav = true;
            console.log(vm.newNav);
            vm.overlay = true;
            vm.saveType = "new";

        }
        //编辑左侧导航
        vm.editLeftNav = function(id){
            var param={id:id}
            _Student.VisitStudent.get(param).then(function (res) {
                if(res.success){
                    vm.newClass = res.data;
                    vm.newNav = true;
                    vm.overlay = true;
                    vm.saveType = "edit"
                    vm.saveNavId = id;
                }
            });

        };
        vm.saveLeftNav = function(){
            if(vm.saveType = "new"){
                var param = vm.newClass;
                _Student.NewClass.get(param).then(function (res) {
                    if(res.success){
                        SweetAlert.swal("新建成功");
                        vm.closeFrame();
                    }
                });
            }else if(vm.saveType = "edit"){
                var param = vm.newClass;
                param.id = vm.saveNavId;
                _Student.EditClass.get(param).then(function (res) {
                    if(res.success){
                        SweetAlert.swal("编辑成功");
                        vm.closeFrame();
                    }
                });
            }
            getLeftbarList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        }
        // 删除左侧导航
        vm.delLeftNav = function(id){
            var param = {id:id}
            _Student.DelClass.get(param).then(function (res) {
                if(res.success){
                    SweetAlert.swal("删除成功");
                    getLeftbarList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
                }
            });
        }

        //保存
        vm.saveNewStu = function () {
            if(vm.saveType = "new"){
                var param = vm.newStu;
                _Student.NewStudent.get(param).then(function (res) {
                    if(res.success){
                        SweetAlert.swal("新建成功")
                        vm.closeFrame();
                    }
                });
            }else if(vm.saveType = "edit"){
                var param = vm.stu;
                param.id = vm.saveStuID;
                _Student.EditStudent.get(param).then(function (res) {
                    if (res.success) {
                        SweetAlert.swal("编辑成功")
                        vm.closeFrame();
                    }
                });
            }
            getLeftbarList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        }
        //编辑学生
        vm.stuItemEdit = function (id) {
            var param={id:id}
            _Student.VisitStudent.get(param).then(function (res) {
                if(res.success){
                    vm.stu = res.data;
                    vm.newFrame = true;
                    vm.overlay = true;
                    vm.saveType = "edit";
                    vm.saveStuID = id;
                }
            });
        }
        //查看学生
        vm.stuItemVis = function (id) {
            var param={id:id}
            _Student.VisitStudent.get(param).then(function (res) {
                if(res.success){
                    vm.stu = res.data;
                    vm.isReadonly = true;
                    vm.newFrame = true;
                    vm.overlay = true;
                }
            });
        }

        //学生信息删除
        vm.stuItemDel = function(id){
            var param={id:id}
            _Student.DelStudent.get(param).then(function (res) {
                SweetAlert.swal("删除成功！");
                if(res.success){
                    for(var i=0;i<vm.student.length;i++){
                        SweetAlert.swal("删除成功！");
                        getLeftbarList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
                    }
                }else{
                    SweetAlert.swal("删除失败！");
                }
            });
        }

        vm.toggleChecked = function (item) {
            if(item === "all"){
                var allCheck = vm.allChecked();
                angular.forEach(vm.student,function (item) {
                    item.check = !allCheck;
                });
            }else {
                item.check = !item.check;
            }
            console.log(item);
        };

        vm.allChecked = function () {
            var allCheck = vm.student && vm.student.length > 0;
            if(allCheck){
                for (var i in vm.student){
                    if(!vm.student[i].check){
                        allCheck = false;
                        break;
                    }
                }
            }
            return allCheck;
        };
    }

})();

